package com.hangsu.train.components.service.impl;

import com.hangsu.train.components.convert.PermissConvert;
import com.hangsu.train.components.dao.PermissDao;
import com.hangsu.train.components.dao.RoleDao;
import com.hangsu.train.components.dao.RolePermissDao;
import com.hangsu.train.components.entity.Permiss;
import com.hangsu.train.components.entity.Role;
import com.hangsu.train.components.params.PermissParam;
import com.hangsu.train.components.params.PermissUpdateParam;
import com.hangsu.train.components.service.PermissService;
import com.hangsu.train.components.vo.PermissVo;
import com.hangsu.train.components.web.utils.PermissUtils;
import com.hangsu.train.core.keygen.KeyGenerate;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author lyy
 * @Date 2021/12/2
 */
@Service
public class PermissServiceImpl implements PermissService {

    private PermissDao permissDao;
    private PermissConvert permissConvert;
    private RoleDao roleDao;
    private RolePermissDao rolePermissDao;
    private KeyGenerate keyGenerate;

    @Resource
    public void setPermissDao(PermissDao permissDao) {
        this.permissDao = permissDao;
    }

    @Resource
    public void setPermissConvert(PermissConvert permissConvert) {
        this.permissConvert = permissConvert;
    }

    @Resource
    public void setRolePermissDao(RolePermissDao rolePermissDao) {
        this.rolePermissDao = rolePermissDao;
    }

    @Resource
    public void setRoleDao(RoleDao roleDao) {
        this.roleDao = roleDao;
    }

    @Resource
    public void setKeyGenerate(KeyGenerate keyGenerate) {
        this.keyGenerate = keyGenerate;
    }

    @Override
    public boolean create(PermissParam param) {
        Permiss permiss = permissConvert.convert(param);
        permiss.setId((Long) keyGenerate.generateKey());
        permissDao.insert(permiss);
        return false;
    }

    @Override
    public boolean del(Long id) {
        permissDao.del(id);
        return true;
    }

    @Override
    public boolean update(PermissUpdateParam param) {
        Permiss permiss = permissConvert.convert(param);
        permissDao.update(permiss);
        return true;
    }

    @Override
    public Permiss detail(Long id) {
        return permissDao.getById(id);
    }

    @Override
    public List<PermissVo> getAllPermiss() {
        List<Permiss> permisses = permissDao.getALL();
        List<Permiss> permissList=PermissUtils.transToTree(permisses);
        return permissList.stream().map(item -> permissConvert.convert(item)).toList();
    }

    @Override
    public List<Permiss> getRolePermiss(String roleCode) {
        Role role = roleDao.getRoleByCode(roleCode);
        if (role != null) {
            List<Long> ids = new ArrayList<>();
            ids.add(role.getId());
            return permissDao.getPermissList(ids);
        }
        return Collections.emptyList();
    }

}
